package com.cloudlead.simple;

import com.cloudlead.utils.persistence.codegen.SimpleMetaDataExporter;
import com.cloudlead.utils.persistence.codegen.serializer.*;
import com.mysema.query.sql.Configuration;
import com.mysema.query.sql.types.DateTimeType;
import com.mysema.query.sql.types.LocalDateType;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.sql.DataSource;
import java.io.File;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.SQLException;

public class DomainCodeGenClient {

    public static void main(String[] args) throws SQLException {
        ApplicationContext ctx = new ClassPathXmlApplicationContext(
                "classpath*:META-INF/spring/*.xml");
        Connection connection = null;
        try {
            DataSource ds = (DataSource) ctx.getBean("dataSource");
            SimpleMetaDataExporter exporter = new SimpleMetaDataExporter();
            exporter.setBeanPrefix("E");
            exporter.setPackageName("com.cloudlead.commonData");
            exporter.setValidationAnnotations(true);
            SimpleBeanSerializer beanSerializer = new SimpleBeanSerializer();
            beanSerializer.setAddToString(true);
            beanSerializer.setPrintSupertype(true);
            exporter.setBeanSerializer(beanSerializer);
            exporter.setMetadataSerializerClass(SimpleMetaDataSerializer.class);
            SimpleVOSerializer voSerializer = new SimpleVOSerializer();
            voSerializer.setAddToString(true);
            voSerializer.setPrintSupertype(true);
            exporter.setVoSerializer(voSerializer);
            SimpleDaoIfcSerializer daoIfcSerializer = new SimpleDaoIfcSerializer();
            exporter.setDaoIfcSerializer(daoIfcSerializer);
            SimpleDaoImplSerializer daoImplSerializer = new SimpleDaoImplSerializer();
            exporter.setDaoImplSerializer(daoImplSerializer);

            SimpleServiceIfcSerializer serviceIfcSerializer = new SimpleServiceIfcSerializer();
            exporter.setServiceIfcSerializer(serviceIfcSerializer);
            SimpleServiceImplSerializer serviceImplSerializer = new SimpleServiceImplSerializer();
            exporter.setServiceImplSerializer(serviceImplSerializer);

            SimpleActionFactorySerializer actionFactorySerializer = new SimpleActionFactorySerializer();
            exporter.setActionFactorySerializer(actionFactorySerializer);

            ValidatorGroupSerializer validatorGroupSerializer = new ValidatorGroupSerializer();
            exporter.setValidatorGroupSerializer(validatorGroupSerializer);

/*            SimpleModulesSerializer modulesSerializer = new SimpleModulesSerializer();
            exporter.setModulesSerializer(modulesSerializer);*/

            exporter.setTargetFolder(new File("src/main/java"));
            connection = ds.getConnection();
            exporter.setInnerClassesForKeys(true);
            Configuration configuration = Configuration.DEFAULT;
            configuration.registerNumeric(10, 2, BigDecimal.class);
            configuration.register(new LocalDateType());
            configuration.register(new DateTimeType());
            exporter.setConfiguration(configuration);
            exporter.setTableNamePattern("tb_fleet_order");
            exporter.export(connection.getMetaData(),
                    connection.createStatement());
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            connection.close();
        }
    }

}
